#import "ToDoGroupDetailView.h"

@implementation ToDoGroupDetailView

- (id)initWithFrame:(struct CGRect)frame
{
	self = [super initWithFrame:frame];
	
  toDoManager = [ToDoManager sharedToDoManager];
	td = [MobileToDoListApplication sharedInstance];
	
	struct CGRect rect = [UIHardware fullScreenApplicationContentRect];
	rect.origin.x = rect.origin.y = 0.0f;
	
	navBar = [[UINavigationBar alloc] init];
	[navBar setFrame:CGRectMake(rect.origin.x, rect.origin.y, rect.size.width, 45.0f)];
	[navBar showButtonsWithLeftTitle:@"Back" rightTitle:nil leftBack:YES];
	[navBar setDelegate:self];
	[navBar enableAnimation];
	
	table = [[UIPreferencesTable alloc] initWithFrame:CGRectMake(0.0f, 45.0f, 320.0f, 415.0f)];
	[table setDataSource:self];
	[table setDelegate:self];
	[table reloadData];
	[self addSubview:navBar];
	[self addSubview:table];
	
	return self;
}

- (void)reloadTableData
{
	[table reloadData];
	[[[table cellAtRow:1 column:0] textField] becomeFirstResponder];
}

- (void)navigationBar:(UINavigationBar*)bar buttonClicked:(int)button
{
	// set data
	[self setData];
	
	// save data now!
	[toDoManager writeToFile];
	
	if([[[table cellAtRow:1 column:0] value] isEqualToString:@""]) {
		[toDoManager removeCurrentGroup];
	}
	
	// back to main view
	[td showToDoGroupsEditWithTransition:2];
}

- (void)setData
{
	[toDoManager setGroup:[[table cellAtRow:1 column:0] value] atIndex:[toDoManager getCurrentGroup]];
}

- (void)alertSheet:(UIAlertSheet *)sheet buttonClicked:(int)button
{
	// now delete for real!
	if (button == 1)
	{
		[toDoManager removeCurrentGroup];
		[td showToDoGroupsEditWithTransition:2];
		// save data now!
		[toDoManager writeToFile];
	}
	
	[sheet dismiss];
}

- (void)tableRowSelected:(NSNotification *)notification
{

	// if delete is clicked!
	if ([table selectedRow] == 3)
	{
		UIAlertSheet *confirmSheet = [[UIAlertSheet alloc] initWithFrame:CGRectMake(0, 240, 320, 240)];
		UIPushButton *deleteButton = [confirmSheet addButtonWithTitle:@"Delete"];
		UIPushButton *cancelButton = [confirmSheet addButtonWithTitle:@"Cancel"];
		[confirmSheet setDestructiveButton:deleteButton];
		[confirmSheet setDefaultButton:cancelButton];
		[confirmSheet setDelegate:self];
		[confirmSheet setAlertSheetStyle:0];
		[confirmSheet presentSheetFromAboveView:self];
	}
}

#pragma mark ----------Datasource Methods-----------

- (int)numberOfGroupsInPreferencesTable:(UIPreferencesTable *)table
{
	return 2;
}

- (int)preferencesTable:(UIPreferencesTable *)table numberOfRowsInGroup:(int)group
{
	if(group == 0) {
		return 1;	
	}
	if(group == 1) {
		return 1;
	}
	
}

- (UIPreferencesTableCell *)preferencesTable:(UIPreferencesTable *)table cellForGroup:(int)group
{
	UIPreferencesTableCell *cell = [[UIPreferencesTableCell alloc] init];
	
	return [cell autorelease];
}

- (UIPreferencesTableCell *)preferencesTable:(UIPreferencesTable *)table cellForRow:(int)row inGroup:(int)group
{
	if (group == 0)
	{
		if (row == 0)
		{
			UIPreferencesTextTableCell *cell;
			cell = [[UIPreferencesTextTableCell alloc] init];
			//[[cell textField] setDelegate:self];
			[cell setTitle:@"Name"];
			[cell setValue:[toDoManager getCurrentGroupName]];
			return [cell autorelease];
			
		}
		if(row == 1) {
			UIPreferencesTextTableCell *cell;
			cell = [[UIPreferencesTextTableCell alloc] init];
			priorityControll = [[UISegmentedControl alloc]
	        initWithFrame:CGRectMake(120.0f, 5.0f, 180.0f, 55.0f)];
	    [ priorityControll insertSegment:0 withTitle:@"1" animated: NO ];
	    [ priorityControll insertSegment:1 withTitle:@"2" animated: NO ];
	    [ priorityControll insertSegment:2 withTitle:@"3" animated: NO ];
	    [ priorityControll insertSegment:3 withTitle:@"4" animated: NO ];
	    [ priorityControll insertSegment:4 withTitle:@"5" animated: NO ];
	    [ priorityControll insertSegment:5 withTitle:@"6" animated: NO ];
			[ priorityControll insertSegment:6 withTitle:@"7" animated: NO ];
			[ priorityControll insertSegment:7 withTitle:@"8" animated: NO ];
			[ priorityControll insertSegment:8 withTitle:@"9" animated: NO ];
	    [ priorityControll selectSegment:[toDoManager getPriorityfromToDo:[toDoManager getCurrentToDo]] ];
			[ cell setTitle:@"Priority" ];
      [ cell addSubview:priorityControll ];
			return [cell autorelease];
		}
		if(row == 2) {
			UIPreferencesTextTableCell *cell;
			cell = [[UIPreferencesTableCell alloc] init];
			[cell setTitle:@"Group"];
			[cell setValue:[toDoManager getGroupNameFromToDo: [toDoManager getCurrentToDo]] ];
			[cell setShowDisclosure:YES];
			return [cell autorelease];
		}
	}
	else {
		if (row == 0)
		{
			UIPreferencesDeleteTableCell *cell = [[UIPreferencesDeleteTableCell alloc] initWithFrame:CGRectMake(0, 0, 320, 45)];
			[cell setTitle:@"Delete Group"];
			[cell setAlignment:2];
		
			CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
			float whiteComponents[4] = {1, 1, 1, 1};
			[[cell titleTextLabel] setColor:CGColorCreate(colorSpace, whiteComponents)];
			
			return [cell autorelease];
		}

	}
}

- (float)preferencesTable:(UIPreferencesTable *)aTable 
    heightForRow:(int)row 
    inGroup:(int)group 
    withProposedHeight:(float)proposed 
{
    if (group == 0) {
        switch (row) {
            case 1 :
                return 55;
                break;
        }
    }
    return proposed;
}

@end